import time

from selenium import webdriver
#加载驱动
from selenium.webdriver.common.by import By

class CaseTest:(object)

def __init__(self):
    self.chrome=webdriver.Chrome()
    self.chrome.get("https://www.baidu.com/")
    self.chrome.maximize_window()

#通过id 去获取元素
def test_id(self):
    self.chrome.find_element(By.ID,"kw").send_keys("nihao")
    self.chrome.find_element(By.ID,"su").click()
    time.sleep(3)
    self.chrome.quit()


#通过name 去定位元素
def test_name(self):
    self.chrome.find_element(By.NAME,"wd").send_keys("nihao")
    self.chrome.find_element(By.ID,"su").click()
    time.sleep(3)
    self.chrome.quit()

#超链接 定位文本
def test_linktext(self):
    time.sleep(3)
    self.chrome.find_element(By.PARIAL_LINK_TEXT,"nihao").click
    self.chrome.quit()

#通过tagname 定位标签
def test_tagname(self):
    time.sleep(3)
    el=self.chrome.find_elements(By.TAG_NAME,"a")
    for i in el:
        print(i)
    self.chrome.quit()

#通过class 定位标签
def test_classname(self):
    time.sleep(3)
    self.chrome.find_element(By.CLASS_NAME,"s_ipt").send_keys("nihao")
    self.chrome.quit()

#通过selector 定位标签
def test_selector(self):
    time.sleep(3)
    self.chrome.find_element(By.CSS_SELECTOR,"#kw").send_keys("nihao")
    self.chrome.quit()

#通过partiallinktext 定位标签
def test_partiallinktext(self):
    time.sleep(3)
    self.chrome.find_element(By.PARTIAL_LINK_TEXT,"上海").click()
    time.sleep(3)
    self.chrome.close()


#注意 xpath 是万能（万金油）的 可以去定位任何东西
#通过xpath 定位标签
def test_xpath(self):
    time.sleep(3)
    self.chrome.find_element(By.XPATH,'//*[@id="kw"]').send_keys("nihao")
    self.chrome.find_element(By.XPATH,"//*[test()='新闻']").click()
    time.sleep(3)
    self.chrome.close()

if __name__ == '__main__':
    test=CaseTest()
    test.test_id()
    test.test_name()
    test.test_linktext()
    test.test_classname()
    test.test_selector()
    test.test_partiallinktext()
    test.test_xpath()